package org.giovanolli.mumi.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.giovanolli.mumi.entities.HelpItem;
import org.giovanolli.mumi.entities.filter.HelpItemFilter;
import org.giovanolli.mumi.service.HelpItemService;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

/**
 * The Class HelpController.
 */
@Controller("helpController")
public class HelpController {

	/** The help item service. */
	@Autowired
	private HelpItemService helpItemService;

	/** The cache. */
	private Map<String, TreeNode> cache = new HashMap<String, TreeNode>();

	/** The help item cache. */
	private Map<String, HelpItem> helpItemCache = new HashMap<String, HelpItem>();

	/** The current help. */
	private HelpItem currentHelp = new HelpItem();

	/**
	 * Generate prime faces tree.
	 * 
	 * @param helpItem
	 *            the help item
	 * @param treeNode
	 *            the tree node
	 * @return the tree node
	 */
	private TreeNode generatePrimeFacesTree(HelpItem helpItem, TreeNode treeNode) {

		TreeNode rv = new DefaultTreeNode(helpItem, treeNode);
		for (HelpItem child : helpItem.getChildren()) {
			generatePrimeFacesTree(child, rv);
		}
		return rv;
	}

	/**
	 * Gets the current help.
	 * 
	 * @return the current help
	 */
	public HelpItem getCurrentHelp() {
		return currentHelp;
	}

	/**
	 * Gets the help content.
	 * 
	 * @param helpId
	 *            the help id
	 * @return the help content
	 */
	@Transactional(readOnly = true)
	public TreeNode getHelpContent(String helpId) {
		TreeNode rv = cache.get(helpId);
		if (rv == null) {
			HelpItemFilter filter = new HelpItemFilter();
			filter.setHelpId(helpId);
			HelpItem helpItem = helpItemService.getList(filter).get(0);
			TreeNode rootNode = new DefaultTreeNode("Root", null);
			rv = generatePrimeFacesTree(helpItem, rootNode);
			rv = rootNode;
			cache.put(helpId, rv);
		}
		return rv;
	}

	/**
	 * Gets the help item.
	 * 
	 * @param helpId
	 *            the help id
	 * @return the help item
	 */
	@Transactional(readOnly = true)
	public HelpItem getHelpItem(String helpId) {
		HelpItem rv = helpItemCache.get(helpId);
		if (rv == null) {
			HelpItemFilter filter = new HelpItemFilter();
			filter.setHelpId(helpId);
			List<HelpItem> list = helpItemService.getList(filter);
			if (list.size() == 1) {
				rv = list.get(0);
				helpItemCache.put(helpId, rv);
			} else {
				rv = new HelpItem();
			}
		}
		currentHelp = rv;
		return rv;
	}

	/**
	 * Gets the root.
	 * 
	 * @return the root
	 */
	public TreeNode getRoot() {
		TreeNode root = new DefaultTreeNode("Root", null);
		TreeNode node0 = new DefaultTreeNode("Node 0", root);
		TreeNode node1 = new DefaultTreeNode("Node 1", root);
		TreeNode node2 = new DefaultTreeNode("Node 2", root);

		TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);
		TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);

		TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);
		TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);

		TreeNode node000 = new DefaultTreeNode("Node 0.0.0", node00);
		TreeNode node001 = new DefaultTreeNode("Node 0.0.1", node00);
		TreeNode node010 = new DefaultTreeNode("Node 0.1.0", node01);

		TreeNode node100 = new DefaultTreeNode("Node 1.0.0", node10);
		return root;
	}

	/**
	 * Retorna valor.
	 * 
	 * @param valor
	 *            the valor
	 * @return the string
	 */
	public String retornaValor(String valor) {
		return "Estoy terornando: " + valor;
	}

	/**
	 * Sets the current help.
	 * 
	 * @param currentHelp
	 *            the new current help
	 */
	public void setCurrentHelp(HelpItem currentHelp) {
		this.currentHelp = currentHelp;
	}
}
